import { ApiProperty, getSchemaPath } from "@nestjs/swagger";
import { CrudValidationGroups } from "@nestjsx/crud";
import { IsOptional, IsString, ValidateNested } from "class-validator";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { file } from "./about/file";
import { BaseEntity } from "./base.entity";
import { Group } from "./group.entity";
import { ResourcesType } from "./resources-type.entity";
import { Village } from "./village.entity";

const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class Resources extends BaseEntity {

  @ApiProperty({
    description: '资源主键',
    required: false
  })
  @PrimaryGeneratedColumn()
  id ?: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => Village)
      }
    ]
  })
  @ManyToOne(() => Village, Village => Village.resources)
  village: Number | Village

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
  })
  @ManyToOne(
    () => Group,
    Group => Group.resources,
    {
      eager: true,
    },
  )
  group: Number | Group;
  

  @ApiProperty({
    example: {},
    description: '资源类型',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => ResourcesType)
      }
    ]
  })
  @ManyToOne(() => ResourcesType, ResourcesType => ResourcesType.resources)
  type :  Number | ResourcesType

  @ApiProperty({
    example: '资源名字',
    description: '资源名字',
    required: true
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  name: string

  @ApiProperty({
    example: '资源简介',
    description: '资源简介',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "text",
    nullable: true
  })
  explanation: string

  @ApiProperty({
    example: '资源位置',
    description: '资源位置',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "char",
    length: 255,
    nullable: true
  })
  position: string

  @ApiProperty({
    example: '占地面积',
    description: '占地面积',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "char",
    length: 255,
    nullable: true
  })
  footprint: string;
  
  @ApiProperty({
    example: '资源图片',
    description: '资源图片',
    required: true
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  image: file [];
}